package erabbit.net.camerabot.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by ziv on 2017/11/3.
 */

public class FileUtils {

    public static final String TAG = "FileUtils";

    public static String saveBitmap(Bitmap bmp, String name) {
        String PHOTO_PATH = Environment.getExternalStorageDirectory().getAbsolutePath();
        File mFile = new File(PHOTO_PATH);
        if (!mFile.exists()) {
            mFile.mkdirs();
        }
        File pictureFile = new File(PHOTO_PATH, name);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(pictureFile);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            //bitmap.recycle();
            fos.close();
            Log.i(TAG, "保存成功URL=" + pictureFile.getAbsolutePath());
            return pictureFile.getAbsolutePath();
        } catch (Exception error) {
            Log.e(TAG, "保存失败");
            error.printStackTrace();
            return null;
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                fos = null;
            }
            return null;
        }
    }


    public static String saveBitmap(Bitmap bmp) {
        String PHOTO_PATH = Environment.getExternalStorageDirectory().getAbsolutePath();
        File mFile = new File(PHOTO_PATH);
        if (!mFile.exists()) {
            mFile.mkdirs();
        }
        Log.i(TAG, "PHOTO_PATH=" + PHOTO_PATH);
        File pictureFile = new File(PHOTO_PATH, getPhotoFileName());
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(pictureFile);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            //bitmap.recycle();
            fos.close();
            Log.i(TAG, "保存成功URL=" + pictureFile.getAbsolutePath());
            return pictureFile.getAbsolutePath();
        } catch (Exception error) {
            Log.e(TAG, "保存失败");
            error.printStackTrace();
            return null;
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                fos = null;
            }
        }
    }


    public static String getPhotoFileName() {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("'LOCK'_yyyyMMdd_HHmmss");
        return dateFormat.format(date) + ".jpg";
    }


    public static Bitmap waterTextMark(Context context, Bitmap bitmap,String text) {
        //添加水印
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        //创建一个bitmap
        Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
        //将该图片作为画布
        Canvas canvas = new Canvas(newBitmap);
        //在画布 0，0坐标上开始绘制原始图片
        canvas.drawBitmap(bitmap, 0, 0, null);
        //在画布上绘制水印图片
        //canvas.drawBitmap(null, 0, 0, null);

        newBitmap = drawTextToRightBottom(context, newBitmap, text, 16, Color.parseColor("#FF0000"));
        // 保存
        canvas.save(Canvas.ALL_SAVE_FLAG);
        // 存储
        canvas.restore();
        return newBitmap;
    }


    /**
     * 绘制文字到中间
     *
     * @param context
     * @param bitmap
     * @param text
     * @param size
     * @param color
     * @return
     */
    public static Bitmap drawTextToCenter(Context context, Bitmap bitmap, String text,
                                          int size, int color) {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(color);
        paint.setTextSize(size);
        Rect bounds = new Rect();
        paint.getTextBounds(text, 0, text.length(), bounds);
        return drawTextToBitmap(context, bitmap, text, paint, bounds,
                (bitmap.getWidth() - bounds.width()) / 2,
                (bitmap.getHeight() + bounds.height()) / 2);
    }


    public static Bitmap drawTextToRightBottom(Context context, Bitmap bitmap, String text,
                                          int size, int color) {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(color);
        paint.setTextSize(size);
        Rect bounds = new Rect();
        paint.getTextBounds(text, 0, text.length(), bounds);
        return drawTextToBitmap(context, bitmap, text, paint, bounds,
                (bitmap.getWidth() - bounds.width()-10) ,
                (bitmap.getHeight() - bounds.height()));
    }

    /**
     * 图片上绘制文字
     */
    private static Bitmap drawTextToBitmap(Context context, Bitmap bitmap, String text,
                                           Paint paint, Rect bounds, int paddingLeft, int paddingTop) {
        Bitmap.Config bitmapConfig = bitmap.getConfig();

        paint.setDither(true); // 获取跟清晰的图像采样
        paint.setFilterBitmap(true);// 过滤一些

        if (bitmapConfig == null) {
            bitmapConfig = Bitmap.Config.ARGB_8888;
        }
        bitmap = bitmap.copy(bitmapConfig, true);
        Canvas canvas = new Canvas(bitmap);

        canvas.drawText(text, paddingLeft, paddingTop, paint);
        return bitmap;
    }

}
